package com.hexus.eve.actions;

import java.util.List;
import java.util.ResourceBundle;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.hexus.eve.entities.Account;
import com.hexus.eve.entities.security.User;
import com.hexus.eve.utils.CurrentUser;
import com.hexus.eve.utils.EmptyItems;
import com.opensymphony.xwork2.ActionSupport;

public class UpdateAccount extends ActionSupport{

	private static final long serialVersionUID = 1L;

	@Autowired
	private SessionFactory sessionFactory;
	
	private Integer id = EmptyItems.INTEGER;
	private String newApiKey = EmptyItems.STRING;
	private Account account;
	private Integer edit = 0;
	private Boolean reload = false;
	private Integer newUserId = EmptyItems.INTEGER;
	
	public String execute() {
		HibernateTemplate ht = new HibernateTemplate(sessionFactory);
		User user = CurrentUser.getUser();
		
		if(id == -1) { // Id is absent - creating new account
			Account a = new Account();
			a.setUser(user);
			a.setApiKey(newApiKey);
			a.setUserId(newUserId);
			ht.save(a);
			user.getAccounts().add(a);
			a.synchronizeWithEve(sessionFactory);
			id = a.getId();
			edit=0;
			reload = true;
		} 
		else { // Update existed account
			List<Account> accounts = user.getAccounts();
	
			for(Account a: accounts) {
				if(a.getId().equals(id)) {
					account = a;
					edit=0;
					account.setApiKey(newApiKey);
					account.setUserId(newUserId);
					ht.update(account);
					account.synchronizeWithEve(sessionFactory);
					return SUCCESS;
				}
			}
			ResourceBundle bundle = ResourceBundle.getBundle ("global", getLocale());
			addActionError(bundle.getString("validation.cant.find.record"));
		}
		
		return SUCCESS;
	}

	@Override
	public void validate() {
		User user = CurrentUser.getUser();
		List<Account> accounts = user.getAccounts();
		
		for(Account a: accounts) {
			if(!a.getId().equals(id)) {
				if(a.getApiKey().equals(newApiKey) || a.getUserId().equals(newUserId) ) {
					ResourceBundle bundle = ResourceBundle.getBundle ("global", getLocale());
					addActionError(bundle.getString("validation.similar.apikey.or.userid"));
				}
			}
		}
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	
	public Account getAccount() {
		return account;
	}

	public void setAccount(Account account) {
		this.account = account;
	}

	public String getNewApiKey() {
		return newApiKey;
	}

	public void setNewApiKey(String newApiKey) {
		this.newApiKey = newApiKey;
	}

	public Integer getEdit() {
		return edit;
	}

	public void setEdit(String edit) {
		if(edit != null && !"".equals(edit))
			this.edit = Integer.parseInt(edit);
	}

	public Boolean getReload() {
		return reload;
	}

	public void setReload(Boolean reload) {
		this.reload = reload;
	}

	public Integer getNewUserId() {
		return newUserId;
	}
	
	public void setNewUserId(String newUserId) {
		try {
			this.newUserId = Integer.parseInt(newUserId);
		} catch (NumberFormatException e) {
		}
	}
	
}
